package PaperExperiment;

import java.util.*;

import XmlLoader.FGObjProcess;
import XmlLoader.FGObjectTrajectoryStructure;

public class RuleBasedAbnormalRetrieval {

	private int screenXsize = 320;
	private int screenYsize = 240;
	private int cellsize;
	private int cellNX = 0;
	private int cellNY = 0;
	private double minSup = 0.2;

	public RuleBasedAbnormalRetrieval() {}
	public RuleBasedAbnormalRetrieval(int cellSize)
	{
		this.cellsize = cellSize ;
		this.cellNX = screenXsize/cellsize ;
		this.cellNY = screenYsize/cellsize ;
	}
	
	public double getsup()
	{
		return minSup;
	}
	// return # cellx and # celly
	public int getCellNX()
	{
		return this.cellNX;
	}
	public int getCellNY()
	{
		return this.cellNY;
	}
	
	public void setScreenSize(int sizeX, int sizeY) {
		this.screenXsize = sizeX;
		this.screenYsize = sizeY;
	}
	public void setSup(double support)
	{
		this.minSup = support;
	}
	public int cellX(int x) //x-coordinate --> cell x
	{
		int i;
		
		for(i = 0; i < screenXsize/cellsize; i++)
		{
			if(x<i*this.cellsize)
				break;
		}
		i--;
		return i;
	}
	public int cellY(int y) //y-coordinate --> cell y
	{
		int i;
		for(i = 0; i < screenYsize/cellsize; i++)
		{
			if(y<i*this.cellsize)
				break;
		}
		i--;
		return i;
	}

	public static void main(String[] args) {
		// construct structured data
		FGObjProcess fgObj = new FGObjProcess(args[1]);
		List<String> list_objIndex = fgObj.ACT004_getStringList(args[0]);
    	List<FGObjectTrajectoryStructure> list = fgObj.ACT001_getListOfObj(list_objIndex);
		
    	RuleBasedAbnormalRetrieval pat = new RuleBasedAbnormalRetrieval(20);
		// int[][] entranceArray = new int[pat.getCellNX()][pat.getCellNY()];
		// int[][] exitArray = new int[pat.getCellNX()][pat.getCellNY()];
		 
		//Cells cell = new Cells();
		Map<String, ArrayList<Integer>> cellMapTraj = new HashMap<String, ArrayList<Integer>>();  //  cell -> Trajectory id
		Map<Integer, ArrayList<Integer[]>> trajMapCell = new HashMap<Integer, ArrayList<Integer[]>>(); // Trajectory id -> cell
		
		
		for (int i = 0; i<list.size();i++)
    	{
    		int size = list.get(i).GET002_nNumberOfPoint();
    		int x = 0, y = 0;
    		Integer [] b = {-1, -1}; // judge repeated
    		for (int j = 0; j<size; j++)
    		{
    			x = list.get(i).GET003_elementIn_list_fGObject(j).GET001_centerReal().x;
    			y = list.get(i).GET003_elementIn_list_fGObject(j).GET001_centerReal().y;
    			// int t = list.get(i).list_fGObject.get(j).tOrigin; //time information
    			Integer[] a = {pat.cellX(x), pat.cellY(y)};
    			
    			if(!(a[0]==b[0]&&a[1]==b[1])) //cells are different 
    			{
    				// traj -> cell
    				ArrayList<Integer[]> l = new ArrayList<Integer[]>();
    				if(trajMapCell.get(i)!=null)
    					l = trajMapCell.get(i);
    				l.add(a);
    				trajMapCell.put(i, l);
    				// cell -> traj
    				String s = a[0].toString()+" "+a[1].toString();
    				ArrayList<Integer> ids = new ArrayList<Integer>();
    				if(cellMapTraj.containsKey(s))
    					ids = cellMapTraj.get(s);
    				if(!ids.contains(i))
    					ids.add(i);
    				
    				cellMapTraj.put(s, ids);
    				//System.out.println(s+" "+ids);
    			}
    			//System.out.println("~ "+cellMapTraj.get(a[0].toString()+a[1].toString()));
    			
    			b = a.clone(); //copy
    			
    			//Cells cell = new Cells();
    			//cell.addID(i);
    			
    			/*if(j == 0)
    				entranceArray[pat.cellX(x)][pat.cellX(y)]++;
    			if(j == size-1)
    				exitArray[pat.cellX(x)][pat.cellX(y)]++;*/
    		}
    	}
		for(int i = pat.getCellNX()-1; i >=0 ; i--)
		{
			for(int j = 0; j < pat.getCellNY(); j++)
			{
				String s = i+" "+j;
				if(cellMapTraj.get(s).size() > list.size()*pat.getsup() )
				{
					System.out.println("("+i+", "+j+")");
					Integer [][] exit = new Integer[pat.cellNX][pat.cellNY];
					for (int x = 0; x < exit.length ; x++)
						for(int y = 0; y<exit[0].length; y++)
						{
							exit[x][y] = new Integer(0);
						}
					
					for(int k = 0; k<cellMapTraj.get(s).size() ;k++)
						exit[trajMapCell.get(cellMapTraj.get(s).get(k)).get(trajMapCell.get(cellMapTraj.get(s).get(k)).size()-1)[0]][trajMapCell.get(cellMapTraj.get(s).get(k)).get(trajMapCell.get(cellMapTraj.get(s).get(k)).size()-1)[1]]++;
					
					for(int m = exit.length-1; m >= 0; m--) { 
			            for(int n = 0; n < exit[0].length; n++) 
			            	if(n!=exit[0].length-1)
			            		System.out.print(exit[m][n] + ",");
			            	else
			            		System.out.print(exit[m][n]);
			            System.out.println(); 
			        } 
				}
			}
		}
		
		
		
		/*for (int i =0;i < trajMapCell.size();i++)
		{
			System.out.println(i+".");
			for (int j =0; j < trajMapCell.get(i).size(); j++)
			if(trajMapCell.get(i).get(j) != null)
				System.out.println("("+trajMapCell.get(i).get(j)[0]+", "+trajMapCell.get(i).get(j)[1]+")");
		}
		for(int i = pat.getCellNX()-1; i >=0 ; i--)
		{
			for(int j = 0; j < pat.getCellNY(); j++)
			{
				//Integer [] a = {i, j};
				String s = i +" "+j;
				System.out.print(cellMapTraj.get(s).size()+",");
			}
			System.out.print("\n");
		}*//*
		System.out.println("Entrance");
		pat.printArray(entranceArray);
		System.out.println("Exit");
		pat.printArray(exitArray);
		*/
	}

}
